package com.broadengate.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.broadengate.bean.ContractInfo;
import com.broadengate.bean.ProjectInfo;
import com.broadengate.dao.BaseDao;
import com.broadengate.dao.ContractDao;
import com.broadengate.dao.IncomeDao;

@Service("incomeService")
public class IncomeService<T> extends BaseService<T>
{
    
    @Autowired
    private IncomeDao<T> incomeDao;
    
    @Autowired
    private ContractDao<T> contractDao;
    
    @Override
    public BaseDao<T> getDao()
    {
        // TODO Auto-generated method stub
        return incomeDao;
    }
    
    /**
     * 获取当前年下的此部门下的所有收支表名称列表
     * <功能详细描述>
     * @param year
     * @param departmentId
     * @return [参数说明]
     * 
     * @return List<String> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public List<String> getIncomeByYearAndDepartmentId(String year, int departmentId)
    {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("year", year);
        param.put("departmentId", departmentId);
        return incomeDao.getIncomeByYearAndDepartmentId(param);
    }
    
    /**
     * 根据收支名称获取收支表的收入合计
     * @param incomeName
     * @return [参数说明]
     * 
     * @return float [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public float getIncomeValueByName(String incomeName)
    {
        return incomeDao.getIncomeValueByName(incomeName);
    }
    
    /**
     * 根据收支表名称获取所有此收支表对应的合同列表
     * @param incomeName
     * @return [参数说明]
     * 
     * @return List<ContractInfo> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public List<ContractInfo> getContractInfoListByIncomeName(String incomeName)
    {
        return incomeDao.getContractInfoListByIncomeName(incomeName);
    }
    
    /**
     * 获取收支表项目的详细信息
     * @param incomeName
     * @return [参数说明]
     * 
     * @return ProjectInfo [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public ProjectInfo getIncomeDetail(String incomeName)
    {
        return incomeDao.getIncomeDetail(incomeName);
    }
    
    /**
     * 取消项目和收支表的关联关系
     * @param contractIds
     * @param incomeName [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public void unRelateContracts(String contractId, String incomeName) throws Exception
    {
        Map<String, String> map = new HashMap<String, String>();
        map.put("incomeName", incomeName);
        map.put("contractId", contractId);
        incomeDao.unRelateContracts(map);
        
        Map<String,String> updateMap = new HashMap<String,String>();
        updateMap.put("po", contractId);
        updateMap.put("isRelated", "未匹配");
        //修改合同状态为未匹配
        contractDao.updateContractRelated(updateMap);
    }
    
    /**
     * 收支 合同关联
     * @param param
     * @throws Exception [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public void releateContrats(String contractId, String incomeName) throws Exception
    {
        Map<String, String> map = new HashMap<String, String>();
        map.put("incomeName", incomeName);
        map.put("contractId", contractId);
        incomeDao.relateContract(map);
        
        
        Map<String,String> updateMap = new HashMap<String,String>();
        updateMap.put("po", contractId);
        updateMap.put("isRelated", "已匹配");
        //修改合同状态为已匹配
        contractDao.updateContractRelated(updateMap);
        
    }
}
